#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>

using namespace std;

int countGoodNodes(vector<vector<int>>& edges) {
    int n = edges.size() + 1;
    vector<vector<int>> g(n);
    for (auto& e : edges)
    {
        int x = e[0], y = e[1];
        g[x].push_back(y);
        g[y].push_back(x);
    }

    int ans = 0;
    auto dfs = [&](auto&& dfs, int x, int fa)->int {
        int size = 1, sz0 = 0;
        bool flag = true;
        for (int y : g[x])
        {
            if (y == fa) continue;
            int sz = dfs(dfs, y, x);
            if (sz0 == 0)
                sz0 = sz;
            else if (sz != sz0)
                flag = false;
            size += sz;
        }
        ans += flag;
        return size;
    };

    dfs(dfs, 0, -1);
    return ans;
}